package com.schooldate.model.service.impl;

import com.schooldate.base.BaseSupportImpl;
import com.schooldate.model.entity.ExamSource;
import com.schooldate.model.service.ExamSourceService;
import org.apache.lucene.search.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by etund on 2015/4/25 0025.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class ExamSourceServiceImpl extends BaseSupportImpl<ExamSource> implements ExamSourceService {

    @Override
    public List<ExamSource> SearchExam(String keyWord, Integer schoolInfoId, Integer departmentId) {
        Session session= this.getSession();
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(ExamSource.class).get();
        //在这里修改要查询的字段
        System.out.println(keyWord);
        Query luceneQuery = queryBuilder.keyword()
                .fuzzy()
                .withThreshold(0.7f)
                .onFields("title", "content")
                .matching(keyWord).createQuery();
        org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, ExamSource.class).setCriteriaQuery(
                this.getSession().createCriteria(ExamSource.class)
                .add(Restrictions.eq("school.id",schoolInfoId))
                .add(Restrictions.eq("department.id",departmentId))
        );
        List<ExamSource> rsList = fullTextQuery.list();
        return rsList;
    }
}
